package cx3k.GUI;

import java.util.Collection;
import java.util.HashSet;

import cx3k.scheduler.objects.util.CompareUtil;


public class AcademicYear implements Comparable<AcademicYear>{

	private int startYear;
	private int endYear;
	
	private Collection<ScheduledSemester> semesters;
	
	public AcademicYear(int startYear, int endYear){
		setStartYear(startYear);
		setEndYear(endYear);
		setSemesters(new HashSet<ScheduledSemester>());
	}
	
	public boolean addSemester(ScheduledSemester semester){
		return getSemesters().add(semester);
	}
	
	public boolean addSemesters(Collection<ScheduledSemester> semesters){
		return getSemesters().addAll(semesters);
	}
	
	public int getStartYear(){
		return startYear;
	}
	
	public int getEndYear(){
		return endYear;
	}
	
	public String getYearNumber(){
		return startYear + " - " + endYear;
	}
	
	public Collection<ScheduledSemester> getSemesters(){
		return semesters;
	}
	
	private void setStartYear(int startYear){
		this.startYear = startYear;
	}
	
	private void setEndYear(int endYear){
		this.endYear = endYear;
	}
	
	private void setSemesters(Collection<ScheduledSemester> semesters){
		this.semesters = semesters;
	}
	
	public boolean removeSemester(ScheduledSemester semester){
		return getSemesters().remove(semester);
	}
	
	@Override
	public boolean equals(Object obj){
		try{
			AcademicYear other = (AcademicYear) obj;
			
			return this.getStartYear() == other.getStartYear() && 
				   this.getEndYear() == other.getEndYear();
			
		} catch(ClassCastException e){
			return false;
		}
	}
	
	@Override
	public String toString(){
		return getYearNumber();
	}

	@Override
	public int compareTo(AcademicYear other) {
		
		int compareStartYears = CompareUtil.compareYears(this.getStartYear(), other.getStartYear());
		
		if(compareStartYears == 0){
			return CompareUtil.compareYears(this.getEndYear(), other.getEndYear());
		} else {
			return compareStartYears;
		}
	}
	
	
	

}
